#include "lpc11xx.h"
#include "msg.h"
					   
TASK_NAME(task_poll1);
TASK(task_led, msg, data)
{
	TASK_BEGIN();   
	//init 
	GPIOSetDir(2, 9, 1);  

	//body
	while(1)
	{ 
		TASK_WAIT_MSG_TO(3);
		do{
		  if(msg == MSG_TIMER ) 	{
				LPC_GPIO2->DATA ^= (0x1<<9);
				task_poll(&task_poll1);
			}
		}while(msg_get(TASK_CURRENT(),&msg,&data));
	}
	
	TASK_END();
}

//*

TASK(task_poll1, msg, data)
{
	TASK_BEGIN();   
	//init 
	GPIOSetDir(2, 10, 1);  

	//body
	while(1)
	{ 
		TASK_WAIT_MSG();
		do{
		  if(msg == MSG_POLL ) 	LPC_GPIO2->DATA ^= (0x1<<10);
		}while(msg_get(TASK_CURRENT(),&msg,&data));
	}
	
	TASK_END();
}

int msg1;
TASK(task_broadcast1, msg, data)
{
	TASK_BEGIN();   
	//body
	msg1=msg_alloc();
	while(1)
	{ 
		TASK_WAIT_MSG_TO(5);
		do{
		  if(msg == MSG_TIMER ) 	msg_post(NULL, msg1,NULL);
		}while(msg_get(TASK_CURRENT(),&msg,&data));
	}
	
	TASK_END();
}

TASK(task_listen, msg, data)
{
	TASK_BEGIN();   
	msg_broadcast_listen(TASK_CURRENT());
	GPIOSetDir(2, 11, 1);  

	//body
	while(1)
	{ 
		TASK_WAIT_MSG();
		do{
		  if(msg == msg1 ) 	LPC_GPIO2->DATA ^= (0x1<<11);
		}while(msg_get(TASK_CURRENT(),&msg,&data));
	}
	
	TASK_END();
}

//
//***
TASK(task_on_off_led, msg, data)
{
	TASK_BEGIN();   
	//init 
	GPIOSetDir(2, 8, 1);  

	//body
	while(1)
	{ 
		TASK_WAIT_MSG();

		do{
		  if(msg == MSG_MSG ) 	LPC_GPIO2->DATA ^= (0x1<<8);
		}while(msg_get(TASK_CURRENT(),&msg,&data));
	}
		
	TASK_END();
}

//*
TASK(task_send, msg, data)
{
	TASK_BEGIN();
	//init
	TASK_WAIT_MSG_TO(10);						 

	while(1){
	//body
	do
	{
		if (msg == MSG_TIMER) {
			msg_post(&task_on_off_led,MSG_MSG,NULL);
			TASK_WAIT_MSG_TO(10);
		}
		else{
		 	TASK_WAIT_MSG();	
		}
	}while(msg_get(TASK_CURRENT(),&msg,&data));
	}
	TASK_END();			
}

TASK(task_no_pt,msg,data)
{
	if(msg==0x20){
			//printf("received\n");
	}
}

TASK(task_send_5ms,msg,data)
{
	TASK_BEGIN();
	while(1){
		TASK_WAIT_MSG_TO(10);
		msg_post(&task_no_pt,0x20,NULL);
	}
	TASK_END();
}

